<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
import Edit from "./../edit/index"
export default {
  mixins: [Vue.prototype.$vFN.Mixins.Base, Vue.prototype.$vFN.Mixins.Page, Vue.prototype.$vFN.Mixins.Table],
  components: {
      ShareQrcodeBox: async () =>
            Vue.extend(
                await Vue.prototype.$vFN.Components.Base.ShareQrcodeBox()
      ),
  },
  data() {
    return {
      appWindow: window,
      // 页面接口地址
      apiUrl: {
        list: "plugin/cashier-desk/admin/setting/special-list",
        delete: "plugin/cashier-desk/admin/setting/special-del",
        settingDetail: "plugin/cashier-desk/admin/setting/detail",
        settingIntegrallist: "plugin/cashier-desk/admin/setting/integral-list",
        settingPayTypelist: "plugin/cashier-desk/admin/setting/pay-type-list",
      },
      // 基础设置
      form: {
        status: 0,
        cost_ratio: 0,
        type: [],
        integral_list: [],
        pay_type_list: [],
      },
      
      // 选中的值
      value: 0,

      isLoading: false,

      // 资产
      integralList: [],
      // 可用抵扣列表
      deductList: [], 
      // 付款方式列表
      paymentList: [],
    };
  },
  methods: {
    // 删除
    async del(_id) {
      let res = await this.$vFN.Http.post({
        url: this.apiUrl.delete,
        data: {
          id: _id,
        }
      },window)
      
      if(res.code == 200) {
         this.$vFN.Tools.messageSuccess("删除成功");
         this.mixinTable_refreshData();
      }
    },

    // 设置
    editBoxOpen(params_) {
      Edit.init({
        title: params_.type == "add" ? "新增设置" : "编辑设置",
        editType: params_.type,
        form_data: this.form,
        deductList: this.deductList.data,
        paymentList: this.paymentList.data,
        onSuccess: () => {
          this.init();
        }
      })
    },

    // 修改
    modifyBoxOpen(params_){
       Edit.init({
         title: "修改",
         editType: params_.type,
         form_data: params_,
         onSuccess: () => {
           this.mixinTable_refreshData();
         }
       })
    },

    // 基础设置获取详情
    async getData() {
      let res = await this.$vFN.Http.post(
        {
          url: this.apiUrl.settingDetail,
        },
        window
      );
       
      for(let key in this.form){
          if(!this.$vFN.Utils.isEmpty(res.data[key])){
            this.form[key] = res.data[key];
          }
      }

    },

    // 获取可用抵扣列表
    async getDeduct(){
      this.integralList = [];

      this.deductList = await this.$vFN.Http.post({
        url: this.apiUrl.settingIntegrallist,
      },window); 
      // 支付方式回显
      // 一个对照表，找对应的值，返回对应的值
      this.form.integral_list.forEach(element => {

       if( !this.$vFN.Utils.isEmpty( this.deductList.data.find(item => item.id === element)?.name )){
            this.integralList.push( this.deductList.data.find(item => item.id === element)?.name );
        }

      });

    },

    // 获取支付方式列表
   async getPayment(){
        this.form.type = [];

        this.paymentList = await this.$vFN.Http.post({
              url: this.apiUrl.settingPayTypelist,
        },window);
        
       // 支付方式回显
       this.form.pay_type_list.forEach(element => {
       
       if( !this.$vFN.Utils.isEmpty( this.paymentList.data.find(item => item.type === element)?.name )){
            this.form.type.push( this.paymentList.data.find(item => item.type === element)?.name );
        }

      });

    },

    // 获取二维码
     showCode() {
      this.$refs.shareQrcodeBoxCashier.init({
                config: {
                    path: "/packSub/cashier/index",
                    query: {
                        a: 2,
                        b: 2,
                    },
                },
            });
     },

    // 初始化
    async init() {
      this.isLoading = true;

      await this.getData();
      await this.getDeduct();

      // 如果是门店不需要列表
      if(this.$vFN.Tools.getProjectType() == 'site'){
       await  this.getPayment();
       await  this.mixinTable_refreshData();
      }

      this.isLoading = false;
    },
    
  },
  mounted() {
    this.init();
  },
  activated() {},
};
</script>